<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/1
 * Time: 12:54
 */

class WebsiteService extends BaseService
{
    public function _initialize()
    {
        parent::_initialize();

        $this->rechargeService = service('recharge');

        $this->mUser = D("User");
        $this->chargingWebsite = D('ChargingWebsite');
        $this->chargingTerminal = D('ChargingTerminal');
        $this->chargingLog = D('ChargingLog');
    }

    /**
     *  * * 附近网点获取
     * @param $latitude 经度
     * @param $longitude 纬度
     * @param $chargingId
     * @return mixed
     */
    public function getNearWebsite($chargingId,$latitude,$longitude) {
        /*$latitude = 30.284052;
        $longitude = 120.13669;*/

        $baidu_location = $this->Convert_GCJ02_To_BD09($latitude, $longitude);//转化成百度地图坐标

        $Distance = new Distance();
        $squares = $Distance->returnSquarePoint( $baidu_location['lng'],$baidu_location['lat'],10000);//计算距离
        $condition = array();
        $condition['charging_id'] = $chargingId;
        $condition['latitude']=array('between',"{$squares['right-bottom']['lat']},{$squares['left-top']['lat']}");
        $condition['longitude']=array('between',"{$squares['left-top']['lng']},{$squares['right-bottom']['lng']}");
        $condition['cut_off'] = 0;

        $recordParam = $this->rechargeService->setRecordParam($condition,10,$this->chargingWebsite);

        $websiteList = $this->chargingWebsite->getList($condition , 'website_id,latitude,longitude,website_name,address',null,null/*,$recordParam['limit']*/); //查询区间范围内的网点信息
        //echo $this->chargingWebsite->getLastSql();

        foreach ($websiteList as $key =>$value){
            //坐标转化为腾讯地图
            $lonArr = $this->Convert_BD09_To_GCJ02($value['latitude'],$value['longitude']);
            $websiteList[$key]['latitude'] = $lonArr['lat'];
            $websiteList[$key]['longitude'] = $lonArr['lng'];

            //获取网点已使用终端信息
            $array = array(
                'website_id' => $value['website_id'],
                'status' =>0, // 0网点开启中
                'type' =>1, //已合闸，表示已使用
            );
            $logInfo = $this->chargingLog->getList($array , 'terminal_id'); //获取终端使用记录

            $terminalInfo = $this->chargingTerminal->getList(array('charging_id'=>$chargingId,'website_id' => $value['website_id'], 'cut_off' => 0)); // 当前网点的终端信息

            foreach($terminalInfo as $k => $v) {
                foreach($logInfo as $k1 => $v1) {
                    if($v1['terminal_id'] == $v['terminal_id']) {
                        $terminalInfo[$k]['using'] = 1;
                    }
                }
            }

            $line_num = array();
            foreach($terminalInfo as $k => $v){
                $line_num[$k] = $v['line_num'];
            }
            array_multisort($line_num,$terminalInfo);
            //array_multisort($distance, SORT_ASC, $data);
            $websiteList[$key]['website'] = $terminalInfo;

            $i = 0;
            if($logInfo){ //当前正在使用的终端
                foreach ($logInfo as $k =>$v){
                    $terminal_id = $this->chargingTerminal->getFieldByTerminalId($v['terminal_id'] , 'terminal_id');
                    if($terminal_id){
                        $i++;
                    }
                }
            }

            //查询网点终端的数量
            $array = array(
                'website_id' => $value['website_id'],
                'cut_off' => 0,
            );

            $websiteList[$key]['count'] = $this->chargingTerminal->getCount($array);//网点终端数量
            if($websiteList[$key]['count'] == 0) {
                unset($websiteList[$key]);
            } else {
                $websiteList[$key]['over_count']=$websiteList[$key]['count']-$i;//剩余终端数量
                $number = $Distance->GetDistance( $baidu_location['lat'],$baidu_location['lng'],$value['latitude'],$value['longitude']);
                $websiteList[$key]['distance']=round($number, 1);//网点与人的距离
            }

        }
        //对网点排序
        $distance=array();
        foreach ($websiteList as $infos){
            $distance[]=$infos['distance'];
        }
        array_multisort($distance, SORT_ASC, $websiteList);
        return array($websiteList,$recordParam);
    }

    /**
     * 网点与人的距离
     * @param $condition
     * @param $latitude
     * @param $longitude
     * @return string
     */
    public function getDistance($condition,$latitude,$longitude) {

        if($latitude && $longitude) {
            //最近一次充电
            $chargingLogInfo = D('ChargingLog')->getInfo($condition,'website_id','create_time desc');
            $websiteInfo = D("ChargingWebsite")->getInfo(array('website_id' => $chargingLogInfo['website_id']));
            if($websiteInfo) {
                $baidu_location = $this->Convert_GCJ02_To_BD09($latitude, $longitude);//转化成百度地图坐标

                $Distance = new Distance();
                $number = $Distance->GetDistance( $baidu_location['lat'],$baidu_location['lng'],$websiteInfo['latitude'],$websiteInfo['longitude']);
                $distance = number_format($number, 1);

                return $distance;
            }
        }

    }

    /**
     * 中国正常GCJ02坐标---->百度地图BD09坐标
     * 腾讯地图用的也是GCJ02坐标
     * @param $lat
     * @param $lng
     * @return array
     */
    function Convert_GCJ02_To_BD09($lat,$lng){
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $lng;
        $y = $lat;
        $z =sqrt($x * $x + $y * $y) + 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) + 0.000003 * cos($x * $x_pi);
        $lng = $z * cos($theta) + 0.0065;
        $lat = $z * sin($theta) + 0.006;
        return array('lng'=>$lng,'lat'=>$lat);
    }


    /**
     * 百度地图BD09坐标---->中国正常GCJ02坐标
     * 腾讯地图用的也是GCJ02坐标
     * @param double $lat 纬度
     * @param double $lng 经度
     * @return array();
     */
    function Convert_BD09_To_GCJ02($lat,$lng){
        $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        $x = $lng - 0.0065;
        $y = $lat - 0.006;
        $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
        $lng = $z * cos($theta);
        $lat = $z * sin($theta);
        return array('lng'=>$lng,'lat'=>$lat);
    }

    /**
     * * 查询区间范围内的网点信息
     * @param $condition
     * @return bool
     */
    public function getWebsite($condition) {
        $websiteList = $this->chargingWebsite->getList($condition , 'website_id,latitude,longitude,website_name'); //
        if($websiteList) {
            return $websiteList;
        } else {
            return false;
        }
    }

}